// 全局js函数封装文件
const digitsRE = /(\d{3})(?=\d)/g
/***
 *JS 将数字转化成为货币格式
 */
export const formatMoney = (value, currency, decimals) =>{
    value = parseFloat(value);
    if (!isFinite(value) || (!value && value !== 0)) return '';
    currency = currency != null ? currency : '$';
    decimals = decimals != null ? decimals : 2;
    let stringified = Math.abs(value).toFixed(decimals)
    let _int = decimals
        ? stringified.slice(0, -1 - decimals)
        : stringified
    let i = _int.length % 3;
    let head = i > 0
        ? (_int.slice(0, i) + (_int.length > 3 ? ',' : ''))
        : '';
    let _float = decimals
        ? stringified.slice(-1 - decimals)
        : '';
    let sign = value < 0 ? '-' : '';
    return (sign + currency + head +
        _int.slice(i).replace(digitsRE, '$1,') +
        _float).replace('$','')
};
/**
 * 日期格式化
 * @param date
 * @param fmt
 * @returns {*}
 */
export const formatDate = (date, fmt) => {
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
};
/**
 * 替换
 * @param arr
 * @param index
 * @return {*}
 */
export const replaceArrayByIndex = (arr, index,obj) => {
    let  length = arr.length
    if (index < 0 || index >= length) {
        throw new Error('Remove failed. Request index >= 0 and index <= length');
    }
    arr.splice(index, 1, obj);
    return arr;
}
/**
 * 时间比较
 * @param d1
 * @param d2
 * @return {boolean}
 */
export const compareDate = (d1,d2) => {
    return ((new Date(d1.replace(/-/g,"\/"))) > (new Date(d2.replace(/-/g,"\/"))));
}
//=============2020-5-14 update
// 找出数组对象某个字符串的最大值/最小值
export const arrObjMaxval = (data, str, status=true) => {
	let arr = [];
	for(let i=0; i < data.length; i++){
		arr.push(data[i][str]);
	}
	let max = Math.max(...arr);
	let min = Math.min(...arr);
	if(status){
		return max
	} else {
		return min
	}
}